<?php



//分页样式
function partienRender($count,$len,$page)
{
	$len=5;
	
	$totolpage = ceil($count/$len);
	
	$pre =$page-1;
	$nex = $page+1;
	$str = '<ul>';
	$str.="<li style='float:left;list-style:none;' ><a
			class='pageclick' style='border:1px solid black;padding:4px'
			  href='index.php?a=".__A__."&b=".__B__."&c=".__C__."&page=1'> 首页</a>
			 	</li>";	
	//前五页
	if($page<=3)
	{
		$start = 1;
		if($totolpage<5)
		{
			$end=$totolpage;
		}else
		{
			$end=5;
		}
		for(;$start<=$end;$start++)
		{
			if($start==$page)
			{
				$str.="<li style='float:left;list-style:none;' ><a
				class='active pageclick' style='border:1px solid black;padding:4px'
				  href='index.php?a=".__A__."&b=".__B__."&c=".__C__."&page={$start}'> 第{$start}页</a>
				 	</li>";		
			}else
			{
				$str.="<li style='float:left;list-style:none;'> <a
				class='pageclick' style='border:1px solid black;padding:4px'
				 href='index.php?a=".__A__."&b=".__B__."&c=".__C__."&page={$start}'> 第{$start}页</a>
				 	</li>";			
			}
		}	
	}
	//最后几页
	else if($page>($totolpage-3))
	{
		$start = $totolpage-4;
		$end = $totolpage;
	
		for(;$start<=$end;$start++)
		{
			if($start==$page)
			{
				$str.="<li style='float:left;list-style:none;' ><a
				class='active pageclick' style='border:1px solid black;padding:4px'
				  href='index.php?a=".__A__."&b=".__B__."&c=".__C__."&page={$start}'> 第{$start}页</a>
				 	</li>";		
			}else
			{
				$str.="<li style='float:left;list-style:none;'> <a
				class='pageclick' style='border:1px solid black;padding:4px'
				 href='index.php?a=".__A__."&b=".__B__."&c=".__C__."&page={$start}'> 第{$start}页</a>
				 	</li>";			
			}
		}	
	}
	
	else
	{
		$end = $page+2;
		$start = $page-2;
		for(;$start<=$end;$start++)
		{
			if($start==$page)
			{
				$str.="<li style='float:left;list-style:none;' ><a
				class='active pageclick' style='border:1px solid black;padding:4px'
				  href='index.php?a=".__A__."&b=".__B__."&c=".__C__."&page={$start}'> 第{$start}页</a>
				 	</li>";		
			}else
			{
				$str.="<li style='float:left;list-style:none;'> <a
				class='pageclick' style='border:1px solid black;padding:4px'
				 href='index.php?a=".__A__."&b=".__B__."&c=".__C__."&page={$start}'> 第{$start}页</a>
				 	</li>";			
			}
		
		}
		
	}
		$str.="<li style='float:left;list-style:none;' ><a
				class='pageclick' style='border:1px solid black;padding:4px'
				  href='index.php?a=".__A__."&b=".__B__."&c=".__C__."&page=".$totolpage."'> 尾页</a>
				 	</li>";		
	$str.='</ul>';	
	return $str;	
}

//分页
function partion($page,$table)
{
	//每页要显示的条数
	$len = 5;
	$page--;

	$start = $len*$page;

	$partion = selectAll($table,1,'*','order by Id desc',"limit $start,$len");
	return $partion;
}

//验证码
function checkcode()
{
	ob_start();
	$re = imagecreatetruecolor(200,50);
	$color = imagecolorallocate($re, 64,224,208);
	$image = imagefilledrectangle($re, 0, 0, 200, 50, $color);
	$x2 = 0;
	$checkcode = '';
	//随机数
	for($i = 0;$i<4;$i++)
	{
		$arr = '0123456789zxcvbnmasdfghjklqwertyuiopZXCVBNMASDFGHJKLQWERTYUIOP';
		$split = str_split($arr);
		$str = $split[mt_rand(0, 61)];
		$color3 = imagecolorallocate($re, 0,0,0);
		$x2 = $x2+40;
		$y2 = mt_rand(30, 40);
		$size = mt_rand(15, 34); 
		$angle = mt_rand(-45, 45);
		imagefttext($re, $size , $angle, $x2, $y2, $color3, 'static/fonts/ACASLONPRO-BOLD.OTF', $str);
		$checkcode = $checkcode.$str;
	}
	$_SESSION['checkcode'] = $checkcode;
	//画点
	for($i = 0;$i<1000;$i++)
	{
		$r = mt_rand(0, 255);
		$g = mt_rand(0, 255);
		$b = mt_rand(0, 255);
		$color1 = imagecolorallocate($re, $r,$g,$b);
		$x = mt_rand(0, 200);
		$y = mt_rand(0, 50); 
		imagesetpixel($re,$x,$y,$color1);
	}
	//画线
	for($i = 0;$i<4;$i++)
	{
		$r1 = mt_rand(0, 255);
		$g1 = mt_rand(0, 255);
		$b1 = mt_rand(0, 255);
		$color2 = imagecolorallocate($re, $r1,$g1,$b1);
		$x1 = mt_rand(0, 200);
		$y1 = mt_rand(0, 50); 
		imagearc($re,$x1,$y1,10000,0,0,180,$color2);
	}
	
	imagepng($re);

	header("Cache-Control: max-age=1, s-maxage=1, no-cache, must-revalidate");
	header("Content-type: image/png;charset=utf8");
	ob_flush();
	imagedestroy($re);
		
}

//上传文件
function upload($file)
{
	$type = $file['type'];
	$type = substr($type,strripos($type,'/')+1);
	

	if(!file_exists('./upload'))
	{
		mkdir('./upload');
	}
	$datefilename = date('ymd',time());
	if(!file_exists('./upload/'.$datefilename))
	{
		mkdir('./upload/'.$datefilename);
	}
	$filename = mt_rand(10000,90000).time();
	$dessrc = './upload/'.$datefilename.'/'.$filename.'.'.$type;
	
	copy($file['tmp_name'],$dessrc);
	return $dessrc;
}

//图片水印
function imagewater($src,$dst)
{
	list($src_w,$src_h) = getimagesize($src);//获取图片尺寸
	//获取目的文件后缀名
	$leng = strripos($dst,'.');
	$leng++;
	$suffix = substr($dst,$leng);
	//获取源文件也就是水印文件的资源
	$src = file_get_contents($src);//将某个文件读取成字符串 
	$src = imagecreatefromstring($src);//将字符串转换成图片
	//回去目的文件资源
	$dst = file_get_contents($dst);
	$dst = imagecreatefromstring($dst);
	//生成图片
	imagecopymerge($dst, $src, 0, 0, 0, 0, $src_w, $src_h, 100);//生成图片水印
	
	$filename = 'upload/'.date('ymd',time()).'/'.mt_rand(10000,99999);
	switch($suffix)
	{
		case 'jpg':imagejpeg($dst,$filename.'.jpg');return $filename.'.jpg';break;
		case 'jpeg':imagejpeg($dst,$filename.'.jpeg');return $filename.'.jpeg';break;
		case 'png':imagejpeg($dst,$filename.'.png');return $filename.'.png';break;
		case 'bmp':imagejpeg($dst,$filename.'.bmp');return $filename.'.bmp';break;
		case 'gif':imagejpeg($dst,$filename.'.gif');return $filename.'.gif';break;
	}
}

//文字水印
function waterstr($src,$waterstr = '默认文字',$size = 80)
{
	$imagestr = file_get_contents($src);
	$image = imagecreatefromstring($imagestr);
	$color = imagecolorallocate($image,255,255,255);//三原色
	imagefttext($image, $size, 0, 100, 100, $color, 'static/fonts/ACASLONPRO-BOLD.OTF',$waterstr);
	
	$leng = strripos($src,'.');
	$leng++;
	$suffix = substr($src,$leng);
	$filename = 'upload/'.date('ymd',time()).'/'.mt_rand(10000,99999);
	switch($suffix)
	{
		case 'jpg':imagejpeg($image,$filename.'.jpg');return $filename.'.jpg';break;
		case 'png':imagejpeg($image,$filename.'.png');return $filename.'.png';break;
		case 'bmp':imagejpeg($image,$filename.'.bmp');return $filename.'.bmp';break;
		case 'gif':imagejpeg($image,$filename.'.gif');return $filename.'.gif';break;
	}
		
}

//压缩图片
function thumb($src)
{
	$imagestr = file_get_contents($src);
	$image = imagecreatefromstring($imagestr);
	list($x,$y) = getimagesize($src);
	
	$dst_image = imagecreatetruecolor($x/2,$y/2);//压缩图片
	imagecopyresized($dst_image, $image, 0, 0, 0, 0, $x/2, $y/2, $x, $y);
	$leng = strripos($src,'.');
	$leng++;
	$suffix = substr($src,$leng);
	$filename = 'upload/'.date('y-m-d',time()).'/'.mt_rand(10000,99999);
	switch($suffix)
	{
		case 'jpg':imagejpeg($dst_image,$filename.'.jpg');break;
		case 'png':imagejpeg($dst_image,$filename.'.png');break;
		case 'bmp':imagejpeg($dst_image,$filename.'.bmp');break;
		case 'gif':imagejpeg($dst_image,$filename.'.gif');break;
	}
	return $filename;
}

//递归分类管理
function getcate($arr,$brr = array(),$pid=0,$grade='|')
{
//	 $arr;
	$grade = '--'.$grade;
	foreach($arr as $k=>$v)
	{
		if($v['pid']==$pid)
		{
			$arr[$k]['name'] = $grade.$v['name'];
			$brr[$k] = $arr[$k];
			$brr = getcate($arr,$brr,$v['Id'],$grade);
		}
	}
	return $brr;	
}


?>